﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nsf.Samples.RetryActivitySample.Client;
using Nsf.Samples.RetryActivitySample.Infrastructure.Actions;

namespace Nsf.Samples.RetryActivitySample.ServiceTest.Actions
{
    [Action(Name = "Create workflow", Description = "Creates a workflow instance of the selected type.")]
    public class CreateWorkflowAction : ActionBase
    {
        private const String WF_TYPE_CODE = "TypeCode";

        protected override ICollection<String> GetKnownArguments()
        {
            var knownArguments =  base.GetKnownArguments();
            knownArguments.Add(WF_TYPE_CODE);
            return knownArguments;
        }

        protected override void ValidateArguments(IDictionary<String, String> args)
        {
            String typeCode;
            if (!args.TryGetValue(WF_TYPE_CODE, out typeCode))
                throw new ValidationException("Argument '{0}' doesn't specified.", WF_TYPE_CODE);
            if (typeCode.Count() == 0)
                throw new ValidationException("Value of the '{0}' doesn't specified.", WF_TYPE_CODE);
            if (!String.Equals(typeCode, "Auto", StringComparison.InvariantCultureIgnoreCase) &&
                !String.Equals(typeCode, "AutoManual", StringComparison.InvariantCultureIgnoreCase) &&
                !String.Equals(typeCode, "Manual", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ValidationException("Unknown type code.");
            }
        }   

        protected override void OnExecute(IDictionary<string, string> args)
        {
            var workflowTypeCode = args[WF_TYPE_CODE];
            using (var client = new WorkflowManagerClient())
            {
                var workflowInstanceId = client.StartWorkflow(workflowTypeCode);
                Console.WriteLine(String.Format("Workflow was created. InstanceId = {0}.", workflowInstanceId));
            }
        }

        protected override void OnHelp()
        {
            throw new NotImplementedException();
        }
    }
}
